package com.heima.wemedia.listener;

import com.alibaba.fastjson.JSONObject;
import com.heima.api.ArticleClient;
import com.heima.model.article.dtos.ApArticleDto;
import com.heima.model.schedule.dtos.Task;
import com.heima.model.topics.WmNewsConstants;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.heima.wemedia.service.impl.WmNewsAutoScanServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class Listener {
    private final WmNewsAutoScanService wmNewsAutoScanService;

    @KafkaListener(topics = WmNewsConstants.NEWS_PUBLISH_TOPIC)
    public void wmNewsPublish(ConsumerRecord<String, String> record, Consumer<String, String> consumer) {
        log.info("接收到消息：{}", record);
        String value = record.value();
        Task task = JSONObject.parseObject(value, Task.class);
        String parameters = task.getParameters();
        WmNews wmNews = JSONObject.parseObject(parameters, WmNews.class);
        //发布文章,并生成文章html
        wmNewsAutoScanService.schedulePublishNews(wmNews);
        log.info("文章发布成功！！！");
        consumer.commitSync();
    }
}
